function m = sausage(pars)
%SAUSAGE sausage map.
%   SAUSAGE(D) creates a sausage map of degree D that maps Chebyshev points 
%   into more evenly spaced ones. 

% Copyright 2011 by The University of Oxford and The Chebfun Developers. 
% See http://www.maths.ox.ac.uk/chebfun/ for Chebfun information.

if length(pars) == 2, 
    d = 9;
else
    d = pars(3);
end

[c cp] = sausagecoeffs(d);
map.for = @(s) polyval(c,s);
map.der = @(s) polyval(cp,s);

% if d<19 && mod(d,2)
%     sausagemap = chebfun(smapinv(d));
%     map.inv = @(x) sausagemap(x);
% else
%     ginv = chebfun(map.inv);
%     sausagemap = chebfun(@(x) roots(ginv-x));
%     map.inv = @(x) sausagemap(x);
%end

a = pars(1); b = pars(2);
scale = @(y) ((b-a)*y+b+a)/2;
rescale = @(x) (2*x-b-a)/(b-a);
scaleder = (b-a)/2;

%m.inv = @(x) map.inv(rescale(x));
m.for = @(y) scale(map.for(y));
m.der = @(y) scaleder*map.der(y);
m.name = 'sausage';
m.par = [pars(1) pars(2) d];
m.inherited = true;

function [c cp] = sausagecoeffs(d)
% Return the polynomial coefficients of sausage map with degree D
c = zeros(1,d+1);
c(d:-2:1) = [1 cumprod(1:2:d-2)./cumprod(2:2:d-1)]./(1:2:d);
c = c/sum(c); 
cp = c(1:d).*(d:-1:1);

function y = smapinv(d)
% This function returns the stored values at Chebyshev points for 
% several low degree (D) sausage maps
switch d
    case 1 
         y = [-1 1];
    case 3 
         y = [-1 -0.9977161079678772 -0.9908569941220566 -0.979400408773778 -0.9633095066599541 -0.9425332967870983 -0.9170074724935027 -0.8866558536416266 -0.8513927855578354 -0.8111269870586735 -0.7657675254287589 -0.7152328093376654 -0.6594636923716941 -0.5984418762488644 -0.532214611273964 -0.4609259173298315 -0.3848528240515666 -0.3044422117697783 -0.2203400520396404 -0.1334016602414863 -0.04467166515771664 0.04467166515771701 0.1334016602414868 0.2203400520396405 0.3044422117697785 0.3848528240515665 0.4609259173298313 0.5322146112739636 0.5984418762488641 0.659463692371694 0.7152328093376655 0.765767525428759 0.8111269870586734 0.8513927855578353 0.8866558536416265 0.9170074724935022 0.9425332967870981 0.963309506659954 0.9794004087737777 0.9908569941220564 0.9977161079678771  1];
    case 5 
         y = [-1 -0.9985197181612726 -0.9940679567583827 -0.9866118678445293 -0.9760963887058087 -0.9624438348990505 -0.9455534786578359 -0.9253012937745332 -0.9015401489880016 -0.8741008712578655 -0.8427947828823613 -0.8074185344836574 -0.7677622739649336 -0.7236223238056907 -0.674819426217288 -0.6212230254929831 -0.5627807568931642 -0.4995502676643701 -0.4317281737762033 -0.3596694917951569 -0.2838917009179811 -0.2050612786982948 -0.1239657804208219 -0.04147836030538124 0.04147836030538127 0.1239657804208219 0.2050612786982947 0.2838917009179812 0.3596694917951566 0.4317281737762033 0.4995502676643699 0.5627807568931639 0.621223025492983 0.6748194262172882 0.7236223238056908 0.7677622739649336 0.8074185344836571 0.8427947828823613 0.8741008712578653 0.9015401489880018 0.9253012937745332 0.9455534786578355 0.9624438348990503 0.9760963887058081 0.9866118678445288 0.9940679567583819 0.9985197181612725 1];
    case 7 
         y = [-1 -0.9986851375756791 -0.9947256672639588 -0.9880766749302645 -0.9786624454135434 -0.9663752711333099 -0.9510740062425291 -0.9325826618304275 -0.9106895331523077 -0.8851476320925361 -0.855677570867471 -0.8219744648434149 -0.783720757719183 -0.7406068373044266 -0.6923604714970418 -0.6387840422433225 -0.5797953578704348 -0.5154645931823524 -0.446038778600455 -0.371948007823053 -0.2937934285543842 -0.2123225219378255 -0.128398597565051 -0.04296876984976487 0.0429687698497649 0.1283985975650511 0.2123225219378256 0.2937934285543843 0.3719480078230534 0.4460387786004547 0.5154645931823524 0.5797953578704345 0.6387840422433224 0.6923604714970416 0.7406068373044266 0.7837207577191831 0.8219744648434149 0.8556775708674713 0.8851476320925367 0.9106895331523078 0.9325826618304276 0.9510740062425287 0.9663752711333102 0.9786624454135437 0.9880766749302641 0.9947256672639586 0.9986851375756787 1];
    case 9 
         y = [-1 -0.9989838369425241 -0.9959226675208018 -0.990778183611535 -0.9834856622276632 -0.9739527103541934 -0.9620576482799676 -0.9476477273557744 -0.930537518885459 -0.9105080201831656 -0.8873073090863048 -0.8606539168272607 -0.8302443941389478 -0.7957666251124368 -0.7569199933488155 -0.7134422128654885 -0.66514047395635 -0.6119221679484914 -0.5538192209427601 -0.4910012900570031 -0.4237766083256359 -0.3525830446034265 -0.2779735615497607 -0.2005991694589558 -0.1211903280725619 -0.04053648101477889 0.04053648101477916 0.1211903280725621 0.2005991694589556 0.2779735615497601 0.3525830446034268 0.4237766083256355 0.4910012900570029 0.55381922094276 0.6119221679484913 0.6651404739563499 0.7134422128654887 0.7569199933488158 0.7957666251124368 0.8302443941389469 0.860653916827261 0.8873073090863046 0.910508020183165 0.9305375188854587 0.9476477273557746 0.9620576482799675 0.9739527103541941 0.9834856622276639 0.9907781836115351 0.9959226675208018 0.9989838369425241 1];
    case 11 
         y = [-1 -0.9991299826206655 -0.9965078308506111 -0.9920969518055464 -0.9858353893136922 -0.9776344197381178 -0.9673767048423347 -0.9549141772056822 -0.9400659716439511 -0.9226169260891951 -0.902317465552097 -0.8788860284733995 -0.8520155009934787 -0.8213851853731988 -0.786679329787643 -0.7476119011143707 -0.7039551585784892 -0.6555674669975495 -0.6024150571925219 -0.5445840880108919 -0.4822827471309381 -0.4158360719453834 -0.3456768501303297 -0.2723345689971561 -0.1964226391073398 -0.1186233755896987 -0.03967049883742402 0.03967049883742441 0.1186233755896985 0.1964226391073397 0.2723345689971556 0.3456768501303292 0.415836071945383 0.4822827471309374 0.5445840880108915 0.6024150571925221 0.6555674669975496 0.7039551585784889 0.7476119011143705 0.7866793297876439 0.8213851853731995 0.8520155009934783 0.8788860284734006 0.902317465552097 0.9226169260891955 0.9400659716439516 0.9549141772056816 0.9673767048423341 0.9776344197381182 0.9858353893136925 0.9920969518055467 0.9965078308506102 0.9991299826206659  1];
    case 13 
         y = [-1 -0.9992966717192008 -0.9971769358024202 -0.9936112978439043 -0.9885498032125155 -0.981920868453226 -0.9736297151274961 -0.9635565136632294 -0.9515544341834451 -0.9374479400321796 -0.9210318563360729 -0.9020719912363553 -0.8803083319377242 -0.8554619639882132 -0.8272466748105388 -0.7953854855088823 -0.7596310468516833 -0.7197872756871643 -0.6757286071599611 -0.6274136465077268 -0.5748918953162804 -0.5183045247858615 -0.4578814220756598 -0.3939364040110929 -0.3268613014526813 -0.2571186696291261 -0.1852326630338142 -0.111777894917018 -0.03736646068287968 0.03736646068287951 0.1117778949170177 0.1852326630338144 0.2571186696291254 0.3268613014526822 0.3939364040110921 0.4578814220756598 0.5183045247858619 0.5748918953162798 0.6274136465077271 0.6757286071599611 0.7197872756871633 0.7596310468516844 0.7953854855088829 0.8272466748105388 0.8554619639882134 0.880308331937724 0.902071991236355 0.9210318563360724 0.9374479400321796 0.9515544341834455 0.9635565136632284 0.9736297151274962 0.9819208684532251 0.9885498032125148 0.9936112978439048 0.9971769358024197  0.9992966717192 1];
    case 15 
         y = [-1 -0.9993809687040737 -0.9975148846332146 -0.9943745411913149 -0.9899138121049812 -0.9840664993060534 -0.9767447756545673 -0.967837313224483 -0.9572072686614261 -0.9446904246057388 -0.9300939689577549 -0.9131966238906023 -0.8937510679910647 -0.8714897175031058 -0.8461347635918663 -0.8174127037202092 -0.7850724108100907 -0.7489043862464385 -0.7087579939365993 -0.6645539152550548 -0.6162907903581711 -0.5640469984363891 -0.5079795193181523 -0.4483214330414795 -0.3853785536340705 -0.3195248952116109 -0.2511964981561243 -0.1808833787452332 -0.1091196634894485 -0.03647217221574628 0.03647217221574633 0.1091196634894485 0.1808833787452337 0.2511964981561243 0.319524895211611 0.38537855363407 0.4483214330414795 0.5079795193181528 0.5640469984363891 0.6162907903581709 0.6645539152550534 0.7087579939366001 0.748904386246437 0.7850724108100917 0.8174127037202091 0.8461347635918663 0.8714897175031037 0.8937510679910657 0.913196623890602 0.9300939689577551 0.9446904246057372 0.9572072686614266 0.9678373132244817 0.9767447756545659 0.9840664993060534 0.989913812104981 0.9943745411913134 0.9975148846332146 0.999380968704074 1];
    case 17 
         y = [-1 -0.9993703808834654 -0.9974707276833051 -0.9942683154881504 -0.9897074844702725 -0.983707990891055 -0.9761627808453799 -0.9669353328312824 -0.9558568575827994 -0.9427238705906109 -0.9272969724983455 -0.9093020471305308 -0.8884353825168628 -0.8643741672857317 -0.8367930597279046 -0.805385889203045 -0.7698894512893404 -0.7301049954280469 -0.68591363830918 -0.6372845304929182 -0.584277394166725 -0.5270420929188042 -0.4658169577921589 -0.4009260564141741 -0.3327747502689486 -0.2618429229100753 -0.1886756685724006 -0.1138715879874293 -0.03806905473171218 0.03806905473171224 0.1138715879874297 0.1886756685724009 0.2618429229100754 0.3327747502689486 0.4009260564141736 0.4658169577921579 0.5270420929188053 0.5842773941667252 0.6372845304929178 0.6859136383091798 0.7301049954280466 0.7698894512893389 0.8053858892030449 0.8367930597279062 0.8643741672857305 0.8884353825168642 0.9093020471305308 0.9272969724983463 0.9427238705906117 0.9558568575827976 0.9669353328312832 0.9761627808453794 0.9837079908910533 0.989707484470273 0.9942683154881503 0.9974707276833084 0.9993703808834641 1];
    otherwise
         error('FUN:sausage:inv','Inverse sausage map data is only stored for d in [1:2:17]')
end
